<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
	<head>
		<title>JavaScript: The Good Parts - Method</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		
		<style type="text/css">
			div#header {
				color: #0066FF;
				font-size: 25pt;
			}
			
			div#content {
				color: #B088FF;
				font-size: 12pt;
			}
		</style>
		<!-- embed jquery by google，藉由google嵌入jquery 1.6.2 -->
		<script type="text/javascript" src="http://www.google.com/jsapi"></script>
		<script type="text/javascript" language="javascript">
			google.load( "jquery", "1.6.2" );
		</script>
		<script type="text/javascript">
			
			/****************************************************************************
			在javascript中，函式的呼叫有四種方式:
			此示範第一種，方法呼叫模式(method invocation pattern)
			****************************************************************************/
			
			
			var myObject = {
				value: 0,
				
				// javascript物件的method
				increment: function( inc ) {
					if( typeof( inc ) === "number" ) {
					
						// this參照指向myObject物件
						this.value += inc;
					} else {
						this.value += 1;
					}
				}
			};
		
			$( document ).ready( function() {
				var $content = $( "#content" );
				
				$content.append( "Before increment() value is <b>" + myObject.value + "</b><br />" );
				
				myObject.increment();  // 呼叫物件的method
				$content.append( "After increment() value is <b>" + myObject.value + "</b><br />" );
				
				myObject.increment( 500 );  // 呼叫物件的method，並傳入引數
				$content.append( "After increment( 500 ) value is <b>" + myObject.value + "</b><br />" );
			});
		</script>
		
	</head>
	
	<body>
		<div title="Chapter04" id="header">JavaScript: The Good Parts - Ch04 Function</div>
		<div title="lab" id="content"></div>
	</body>
</html>